import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import '../../api/request/UserInfoRequest.dart';
import '../../utils/Toast/MyTosat.dart';
import '../../utils/Tool/MyTools.dart';
import 'package:get/get.dart';

import '../../api/ApiUtils.dart';
import '../../api/request/LoginRegisterRequest.dart';
import '../../base/MyScaffold.dart';
import '../../res.dart';
import '../../utils/Tool/TimerClass.dart';
import '../../utils/color/ColorUtils.dart';
import '../../widgets/MyLocalImage.dart';
import '../../widgets/TextView.dart';

class ChangePassword extends StatefulWidget {
  const ChangePassword({Key? key}) : super(key: key);

  @override
  State<ChangePassword> createState() => _ChangePasswordState();
}

class _ChangePasswordState extends State<ChangePassword> {
  TextEditingController Controller1 = TextEditingController();
  TextEditingController Controller2 = TextEditingController();
  TextEditingController Controller3 = TextEditingController();
  TextEditingController Controller4 = TextEditingController();
  String text1 = "";
  String text2 = "";
  String text3 = "";
  String text4 = "";
  String? ssid;
  String email = "";
  String _countdownSeconds = "send_code".tr;
  int count = 60;
  bool can = false;
  late TimerClass timerClass;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    timerClass = TimerClass();
  }

  Send() {
    if (MyTools.stringIsEmpty(text1)) {
      MyToast().show("empty1".tr, icon_tips: false);
      return;
    }
    if (!GetUtils.isEmail(text1)) {
      MyToast().show("email_format".tr, icon_tips: false);
      return;
    }
    if (MyTools.stringIsEmpty(text2) || MyTools.stringIsEmpty(text3)) {
      MyToast().show("empty2".tr, icon_tips: false);
      return;
    }
    if (text2 != text3) {
      MyToast().show("two_different".tr);
      return;
    }
    EasyLoading.show(status: "loading", dismissOnTap: true);
    apiShow(
        api: send(text1!, "MAIL_CODE_FINDPW"),
        success: (result) {
          print(result.data);
          ssid = result.data["ssid"];
          _startCountdown();
          EasyLoading.dismiss();
        },
        failure: (e) {
          MyToast().show(e.msg, icon_tips: false);
          EasyLoading.dismiss();
        });
  }

  submit() {
    if (MyTools.stringIsEmpty(text1)) {
      MyToast().show("empty1".tr, icon_tips: false);
      return;
    }
    if (!GetUtils.isEmail(text1)) {
      MyToast().show("email_format".tr, icon_tips: false);
      return;
    }
    if (MyTools.stringIsEmpty(text2) || MyTools.stringIsEmpty(text3)) {
      MyToast().show("empty2".tr, icon_tips: false);
      return;
    }
    if (text2 != text3) {
      MyToast().show("two_different".tr);
      return;
    }
    if (MyTools.stringIsEmpty(text4)) {
      MyToast().show("empty3".tr, icon_tips: false);
      return;
    }
    EasyLoading.show(status: "loading", dismissOnTap: true);
    apiShow(
        api: findpw(
            email: text1,
            password: generateMD5(text3!),
            code: text4,
            ssid: ssid),
        success: (result) {
          EasyLoading.dismiss();
          Get.back();
        },
        failure: (e) {
          MyToast().show(e.msg, icon_tips: false);
          EasyLoading.dismiss();
        });
  }

  isOK() {
    if (!MyTools.stringIsEmpty(text1) &&
        !MyTools.stringIsEmpty(text2) &&
        !MyTools.stringIsEmpty(text3) &&
        !MyTools.stringIsEmpty(text4)) {
      can = true;
      setState(() {});
    }
  }

  // 验证码倒计时时间，单位为秒

  void _startCountdown() {
    timerClass.start(count, (time) {
      count = time;
      _countdownSeconds = "${"resend".tr} $time s";
      if (time == 0) {
        count = 60;
        _countdownSeconds = "resend".tr;
      }
      setState(() {});
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    timerClass.stop();
  }

  @override
  Widget build(BuildContext context) {
    return MyScaffold(
      bottomline: false,
      body: GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: () {
          FocusScope.of(context).requestFocus(FocusNode());
        },
        child: Container(
          alignment: Alignment.center,
          padding: const EdgeInsets.symmetric(vertical: 24, horizontal: 32),
          child: Column(
            children: [
              TextView(
                "reset".tr,
                alignment: Alignment.centerLeft,
                style: TextStyle(
                    color: rgba(48, 49, 51, 1),
                    fontSize: 24,
                    fontWeight: FontWeight.bold),
                // onclick: () {
                //   Get.to(Bind(
                //     name: "",
                //     url: "",
                //     email: "",
                //     token: "",
                //   ));
                // },
              ),
              Container(
                // color: Colors.amber,
                margin: const EdgeInsets.only(top: 24),
                constraints: const BoxConstraints(maxHeight: 60),
                alignment: Alignment.center,
                child: TextField(
                  controller: Controller1,
                  onChanged: (v) {
                    isOK();
                    setState(() {
                      text1 = v;
                    });
                  },
                  decoration: InputDecoration(
                    suffixIcon: Padding(
                      padding: const EdgeInsets.all(14),
                      child: SizedBox(
                        width: 50,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.end,
                          children: [
                            Offstage(
                              offstage: text1.isEmpty ? true : false,
                              child: MyLocalImage(
                                Res.sys_icon_delete_grey3x,
                                width: 16,
                                height: 16,
                                onclick: () {
                                  Controller1.clear();
                                  setState(() {
                                    text1 = "";
                                  });
                                },
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                    enabledBorder: OutlineInputBorder(
                        borderSide: BorderSide(
                      color: rgba(230, 231, 235, 1),
                      width: 1,
                    )),
                    focusedBorder: OutlineInputBorder(
                      borderSide: BorderSide(
                        color: rgba(150, 151, 153, 1), //边框颜色为白色
                        width: 1, //宽度为5
                      ),
                      borderRadius: const BorderRadius.all(
                        Radius.circular(8), //边角为30
                      ),
                    ),
                    hintText: 'email'.tr,
                    hintStyle:
                        TextStyle(color: rgba(200, 202, 204, 1), height: 1),
                  ),
                ),
              ),
              Container(
                // color: Colors.amber,
                margin: const EdgeInsets.only(top: 24),
                constraints: const BoxConstraints(maxHeight: 60),
                alignment: Alignment.center,
                child: TextField(
                  controller: Controller2,
                  obscureText: true,
                  onChanged: (v) {
                    isOK();
                    setState(() {
                      text2 = v;
                    });
                  },
                  decoration: InputDecoration(
                    suffixIcon: Padding(
                      padding: const EdgeInsets.all(14),
                      child: SizedBox(
                        width: 50,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.end,
                          children: [
                            Offstage(
                              offstage: text2.isEmpty ? true : false,
                              child: MyLocalImage(
                                Res.sys_icon_delete_grey3x,
                                width: 16,
                                height: 16,
                                onclick: () {
                                  Controller2.clear();
                                  setState(() {
                                    text2 = "";
                                  });
                                },
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                    enabledBorder: OutlineInputBorder(
                        borderSide: BorderSide(
                      color: rgba(230, 231, 235, 1),
                      width: 1,
                    )),
                    focusedBorder: OutlineInputBorder(
                      borderSide: BorderSide(
                        color: rgba(150, 151, 153, 1), //边框颜色为白色
                        width: 1, //宽度为5
                      ),
                      borderRadius: const BorderRadius.all(
                        Radius.circular(8), //边角为30
                      ),
                    ),
                    hintText: 'new_password'.tr,
                    hintStyle:
                        TextStyle(color: rgba(200, 202, 204, 1), height: 1),
                  ),
                ),
              ),
              Container(
                // color: Colors.amber,
                margin: const EdgeInsets.only(top: 24),
                constraints: const BoxConstraints(maxHeight: 60),
                alignment: Alignment.center,
                child: TextField(
                  controller: Controller3,
                  obscureText: true,
                  onChanged: (v) {
                    isOK();
                    setState(() {
                      text3 = v;
                    });
                  },
                  decoration: InputDecoration(
                    suffixIcon: Padding(
                      padding: const EdgeInsets.all(14),
                      child: SizedBox(
                        width: 50,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.end,
                          children: [
                            Offstage(
                              offstage: text3.isEmpty ? true : false,
                              child: MyLocalImage(
                                Res.sys_icon_delete_grey3x,
                                width: 16,
                                height: 16,
                                onclick: () {
                                  Controller3.clear();
                                  setState(() {
                                    text3 = "";
                                  });
                                },
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                    enabledBorder: OutlineInputBorder(
                        borderSide: BorderSide(
                      color: rgba(230, 231, 235, 1),
                      width: 1,
                    )),
                    focusedBorder: OutlineInputBorder(
                      borderSide: BorderSide(
                        color: rgba(150, 151, 153, 1), //边框颜色为白色
                        width: 1, //宽度为5
                      ),
                      borderRadius: const BorderRadius.all(
                        Radius.circular(8), //边角为30
                      ),
                    ),
                    hintText: 'confirm_password'.tr,
                    hintStyle:
                        TextStyle(color: rgba(200, 202, 204, 1), height: 1),
                  ),
                ),
              ),
              Container(
                // color: Colors.amber,
                margin: const EdgeInsets.only(top: 24),
                constraints: const BoxConstraints(maxHeight: 60),
                alignment: Alignment.center,
                child: TextField(
                  keyboardType: TextInputType.phone,
                  controller: Controller4,
                  onChanged: (v) {
                    isOK();
                    setState(() {
                      text4 = v;
                    });
                  },
                  decoration: InputDecoration(
                    suffixIcon: Padding(
                      padding: const EdgeInsets.all(14),
                      child: SizedBox(
                        width: 50,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.end,
                          children: [
                            Offstage(
                              offstage: text4.isEmpty ? true : false,
                              child: MyLocalImage(
                                Res.sys_icon_delete_grey3x,
                                width: 16,
                                height: 16,
                                onclick: () {
                                  Controller4.clear();
                                  setState(() {
                                    text4 = "";
                                  });
                                },
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                    enabledBorder: OutlineInputBorder(
                        borderSide: BorderSide(
                      color: rgba(230, 231, 235, 1),
                      width: 1,
                    )),
                    focusedBorder: OutlineInputBorder(
                      borderSide: BorderSide(
                        color: rgba(150, 151, 153, 1), //边框颜色为白色
                        width: 1, //宽度为5
                      ),
                      borderRadius: const BorderRadius.all(
                        Radius.circular(8), //边角为30
                      ),
                    ),
                    hintText: 'verification_code'.tr,
                    hintStyle:
                        TextStyle(color: rgba(200, 202, 204, 1), height: 1),
                  ),
                ),
              ),
              TextView(
                _countdownSeconds,
                alignment: Alignment.centerRight,
                padding: const EdgeInsets.all(6),
                margin: const EdgeInsets.only(top: 10),
                style: TextStyle(
                    color: count == 60 || count == 0
                        ? rgba(23, 70, 143, 1)
                        : rgba(200, 202, 204, 1)),
                onclick: () {
                  if (count == 60 || count == 0) {
                    Send();
                  }
                },
              ),
              const SizedBox(
                height: 24,
              ),
              // MaterialButton(
              //   onPressed: () {
              //     // Get.to(const Verification(), arguments: email);
              //     // MyToast().show("registration success");
              //     submit();
              //   },
              //   minWidth: double.infinity,
              //   color: rgba(255, 78, 77, can ? 1 : 0.6),
              //   height: 48,
              //   shape: RoundedRectangleBorder(
              //     borderRadius: BorderRadius.circular(6),
              //   ),
              //   child: const TextView(
              //     "Submit",
              //     style: TextStyle(color: Colors.white),
              //   ),
              // ),
              Container(
                alignment: Alignment.center,
                height: 48,
                decoration: BoxDecoration(
                    color: rgba(255, 78, 77, can ? 1 : 0.3),
                    borderRadius: BorderRadius.circular(6)),
                child: TextView(
                  "submit".tr,
                  alignment: Alignment.center,
                  style: const TextStyle(color: Colors.white),
                  onclick: () {
                    if (can) {
                      submit();
                    }
                  },
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
